package com.app.utils.util;

import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.util.List;

/**
 * APP信息类
 *
 * @author xuxinghai
 */
public class AppTools {

    private static int versionCode;// 版本号
    private static String versionName;// 版本名字
    private static String imei;//设备IMEI号
    private static int sdk_vertioncode;//sdk版本号



    /**
     * [获取应用程序版本名称信息]
     *
     * @return 当前版本的名称
     */
    public static String getVersionName() {
        if (TextUtils.isEmpty(versionName)) {
            try {
                PackageManager packageManager = AppUtils.getInstance().getContext().getPackageManager();
                PackageInfo packageInfo = packageManager.getPackageInfo(
                        AppUtils.getInstance().getContext().getPackageName(), 0);
                versionName = packageInfo.versionName;
            } catch (NameNotFoundException e) {
                e.printStackTrace();
                versionName = "error";
            }
        }
        return versionName;
    }

    /**
     * [获取应用程序版本号信息]
     *
     * @return 当前版本的名称
     */
    public static int getVersionCode() {
        if (versionCode == 0) {
            try {
                PackageManager packageManager = AppUtils.getInstance().getContext()
                        .getPackageManager();
                PackageInfo packageInfo = packageManager.getPackageInfo(
                        AppUtils.getInstance().getContext().getPackageName(), 0);
                versionCode = packageInfo.versionCode;
            } catch (NameNotFoundException e) {
                e.printStackTrace();
                versionCode = 0;
            }
        }
        return versionCode;
    }


    /**
     * 返回设备IMEI号
     *
     * @return
     */
    public static String getIMEI() {
        //检查权限

        if(ContextCompat.checkSelfPermission(AppUtils.getInstance().getContext(),Manifest.permission.READ_PHONE_STATE)
                == PackageManager.PERMISSION_GRANTED){//授权
            if (TextUtils.isEmpty(imei)) {
                TelephonyManager tm = (TelephonyManager) AppUtils.getInstance().getContext()
                        .getSystemService(Context.TELEPHONY_SERVICE);
                if (tm != null) {
                    imei = tm.getDeviceId();//String
                }
            }
        }else{
            imei = "";
        }

        return imei;
    }

    /**
     * 返回终端信息
     * @return
     */
    public static String getAerminal(){
       String mode = Build.MODEL;//手机型号
        String sysVer = Build.VERSION.RELEASE;//系统版本
        String sdkVER = Build.VERSION.SDK;
        return mode+"--"+sysVer+"--"+sdkVER;
    }

    /**
     * 返回手机型号厂商
     * @return
     */
    public static String getPhoneManufacturer(){
        String man = Build.MANUFACTURER;//手机型号
        String model = Build.MODEL;//系统版本
        return man+model;
    }

    /**
     * 获取当前的系统版本
     * @return
     */
    public static int getSdkVertion(){
        if(sdk_vertioncode == 0){
            sdk_vertioncode = Build.VERSION.SDK_INT;
        }
        return sdk_vertioncode;
    }

    /**
     * 根据包名检测程序是否安装
     *
     * @param context
     * @param pkgName
     * @return
     */
    public static boolean isAppInstall(Context context, String pkgName) {
        if (context == null || TextUtils.isEmpty(pkgName)) {
            return false;
        }
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(pkgName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            packageInfo = null;
            e.printStackTrace();
        }
        if (packageInfo == null) {
            //没有安装
            return false;
        } else {
            //已安装
            return true;
        }
    }

    /**
     * 启动app
     *
     * @param context
     * @param pkgName
     */
    public static void startUpApp(Context context, String pkgName) {
        PackageInfo pi = null;
        try {
            pi = context.getPackageManager().getPackageInfo(pkgName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }

        if(pi == null){
            return;
        }
        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resolveIntent.setPackage(pi.packageName);
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);

        ResolveInfo ri = apps.iterator().next();
        if (ri != null) {
            String packageName1 = ri.activityInfo.packageName;
            String className = ri.activityInfo.name;

            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);

            ComponentName cn = new ComponentName(packageName1, className);

            intent.setComponent(cn);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }
}
